<template>
  <div class="page-bg">
    <el-divider></el-divider>
    <div class="weui-cell">
      <div class="weui-cell__hd"><label class="weui-label">所属课题</label></div>
      <div class="weui-cell__bd">
        <div class="weui_cell_bd weui_cell_primary">
          {{mission.moduleName}}
        </div>
      </div>
    </div>
    <div class="weui-cell">
      <div class="weui-cell__hd"><label class="weui-label">任务名称</label></div>
      <div class="weui-cell__bd">
        <div class="weui_cell_bd weui_cell_primary">
          {{mission.name}}
        </div>
      </div>
    </div>
    <div class="weui-cell">
      <div class="weui-cell__hd">
        <label class="weui-label">开始时间</label>
      </div>
      <div class="weui-cell__bd">
        <div class="weui_cell_bd weui_cell_primary">
          {{mission.startTime}}
        </div>
      </div>
    </div>
    <div class="weui-cell">
      <div class="weui-cell__hd">
        <label class="weui-label">结束时间</label>
      </div>
      <div class="weui-cell__bd">
        <div class="weui_cell_bd weui_cell_primary">
          {{mission.endTime}}
        </div>
      </div>
    </div>
    <div class="weui-cell">
      <div class="weui-cell__hd">
        <label class="weui-label">任务负责人</label>
      </div>
      <div class="weui-cell__bd">
        <div class="weui_cell_bd weui_cell_primary">
          {{mission.ownerName}}
        </div>
      </div>
    </div>
    <el-divider></el-divider>
    <div class="row">
      <span class="v-center">
          <svg class="icon taskIcon" aria-hidden="true">
            <use xlink:href="#iconrenwu"></use>
          </svg>
          任务内容
        </span>
    </div>
    <el-divider></el-divider>
    <div class="weui-cell">{{mission.content}}</div>
    <el-divider></el-divider>
    <div class="row">
      <span class="v-center">
          <svg class="icon taskIcon" aria-hidden="true">
            <use xlink:href="#iconrenwu1"></use>
          </svg>
          考核指标
        </span>
    </div>
    <el-divider></el-divider>
    <div class="weui-cell">{{mission.target}}</div>
    <el-divider></el-divider>
    <div class="row">
      <svg class="icon addTaskIcon" aria-hidden="true" @click="handleAddGain" v-if="level === student">
        <use xlink:href="#iconicon_add"></use>
      </svg>
      <span class="v-center">
        <svg class="icon taskIcon" aria-hidden="true">
          <use xlink:href="#iconwancheng"></use>
        </svg>
        成果
      </span>
    </div>
    <el-divider></el-divider>
    <div v-for="item of gainList" :key="item.id"  v-if="item.state === GainYes">
      <div class="row" @click="goGain(item)">
          <span class="taskName">
            <svg class="icon projectIcon" aria-hidden="true">
              <use xlink:href="#icondian3"></use>
            </svg>
            {{item.name}}
          </span>
        <span class="delIcon">
            <svg class="icon tongguoIcon" aria-hidden="true">
              <use xlink:href="#icontongguo1"></use>
            </svg>
          </span>
      </div>
      <el-divider></el-divider>
    </div>
    <div v-for="item of gainList" :key="item.id"  v-if="item.state === GainWaitForCheck">
      <div class="row"  @click="goGain(item)">
          <span class="taskName gainWaitForCheck">
            <svg class="icon projectIcon" aria-hidden="true">
              <use xlink:href="#icondian3"></use>
            </svg>
            {{item.name}}
          </span>
        <span class="delIcon gainWaitForCheck">
            待审核
          </span>
      </div>
      <el-divider></el-divider>
    </div>
    <div v-for="item of gainList" :key="item.id"  v-if="item.state === GainNo">
      <div class="row"  @click="goGain(item)">
          <span class="taskName gainNo">
            <svg class="icon projectIcon" aria-hidden="true">
              <use xlink:href="#icondian3"></use>
            </svg>
            {{item.name}}
          </span>
        <span class="delIcon gainNo">
            被驳回
          </span>
      </div>
      <el-divider></el-divider>
    </div>
    <el-dialog title="新建任务" :visible.sync="addGain" width="90%">
      <div class="weui-cell">
        <div class="weui-cell__hd"><label class="weui-label">成果名称</label></div>
        <div class="weui-cell__bd ">
          <input class="weui-input" pattern="[0-9]*" placeholder="请输入成果名称" type="name" v-model="gain.Name">
        </div>
      </div>
      <div class="weui-cell weui-cells_form">
        <div class="weui-cell__hd">
          <label class="weui-label">备注</label>
        </div>
        <el-input
          type="textarea"
          :rows="4"
          placeholder="对该成果进行简要阐述"
          v-model="gain.Remark"
          clearable>
        </el-input>
      </div>
      <div class="weui-cell">
        <div class="weui-cell__hd"><label class="weui-label">上传附件</label></div>
        <el-upload
          class="upload-demo"
          ref="upload"
          action="http://bci.renjiwulian.com/gain/"
          :data="gain"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :on-success="uploadSuccess"
          :on-error="uploadError"
          :file-list="fileList"
          :auto-upload="false"
          :multiple="false"
          limit=1>
          <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
        </el-upload>
      </div>
      <el-divider></el-divider>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addGain = false">取 消</el-button>
        <el-button type="primary" @click="submitGain()">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Cookies from 'js-cookie'
import axios from 'axios'
export default {
  name: 'missionDetailDetail',
  data () {
    return {
      fileList: [],
      gainID: '',
      level: parseInt(Cookies.get('level')),
      imageUrl: '',
      gain: {
        MissionID: parseInt(sessionStorage.getItem('missionID')),
        Name: '',
        Remark: ''
      },
      addGain: false,
      mission: [],
      gainList: []
    }
  },
  methods: {
    uploadSuccess () {
      alert('上传成功！')
      this.getGainList()
    },
    uploadError () {
      alert('上传失败！请稍后再试或联系助理')
    },
    goGain (item) {
      sessionStorage.setItem('gainName', item.name)
      sessionStorage.setItem('gainID', item.id)
      this.$router.push('/gain')
    },
    handleAvatarSuccess (res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload (file) {
      const isLt2M = file.size / 1024 / 1024 / 1024 < 3

      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 3MB!')
      }
      return isLt2M
    },
    handleRemove (file, fileList) {
      console.log(file, fileList)
    },
    handlePreview (file) {
      console.log(file)
    },
    handleAddGain () {
      this.gain.Name = ''
      this.gain.Remark = ''
      this.addGain = true
    },
    submitGain () {
      this.$refs.upload.submit()
      this.addGain = false
      axios.get(this.url + '/gain/mission/' + sessionStorage.getItem('missionID'))
        .then(this.getGainListSucc)
    },
    getGainList () {
      axios.get(this.url + '/gain/mission/' + sessionStorage.getItem('missionID'))
        .then(this.getGainListSucc)
    },
    getGainListSucc (res) {
      if (res.status === 200) {
        res = res.data
        this.gainList = res
      }
    },
    getMission () {
      axios.get(this.url + '/mission/id/' + sessionStorage.getItem('missionID'))
        .then(this.getMissionSucc)
    },
    getMissionSucc (res) {
      if (res.status === 200) {
        res = res.data
        this.mission = res
      }
    }
  },
  mounted () {
    this.getMission()
    this.getGainList()
  }
}
</script>

<style lang="stylus" scoped>
  .taskName
    margin-left .2rem
  .row
    margin-top .6rem
    margin-bottom .6rem
  .taskIcon
    margin-right .5rem
    color #16b0ff
  .delIcon
    float right
    margin-right .5rem
  .projectIcon
    margin-right .2rem
  .addTaskIcon
    float: right
    font-size 19.9px
    margin-right .9rem
    margin-top .18rem
    color black
  .tongguoIcon
    font-size 29px
  .gainWaitForCheck
    color #7a7b6a
  .gainNo
    color red
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
